package com.craftcomputing.client.Header;

import com.craftcomputing.client.*;
import com.craftcomputing.client.Listeners.SearchResultListeners.*;
import com.craftcomputing.client.Listeners.HeaderListeners.*;
import com.craftcomputing.client.SearchResult.*;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

public class Header extends Composite {

	public Header() {		
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("800px");
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		//final Label playfulInventionDatabaseLabel = new Label("Playful Invention Database");
		final Image playfulInventionDatabaseLabel = new Image("mainimages/header3a.gif");
		playfulInventionDatabaseLabel.addClickListener(new HeaderClickListener());
		playfulInventionDatabaseLabel.setStyleName("gwt-ImageLink");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		final RoundedPanel roundedPanel = new RoundedPanel(horizontalPanel_2, RoundedPanel.TOP, 9);
		roundedPanel.setCornerColor("#f07d1e");
		verticalPanel.add(roundedPanel);
		//playfulInventionDatabaseLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		playfulInventionDatabaseLabel.setSize("744px", "110px");
		horizontalPanel_2.setSize("800px", "100%");
		horizontalPanel_2.add(playfulInventionDatabaseLabel);
		horizontalPanel_2.setStyleName("gwt-Header");

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("700px");
		horizontalPanel.setStyleName("gwt-Header");
		verticalPanel.setCellHorizontalAlignment(horizontalPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		horizontalPanel.add(horizontalPanel_4);
		horizontalPanel_4.setSize("400px", "50px");

		final Label padding = new Label("");
		horizontalPanel_4.add(padding);
		padding.setSize("16px", "50px");
		horizontalPanel_4.setCellHorizontalAlignment(padding, HasHorizontalAlignment.ALIGN_LEFT);

		final Label padding2 = new Label("");
		horizontalPanel_2.insert(padding2, 0);
		padding2.setSize("20px", "100%");

		final Image image = new Image("mainimages/header3b.gif");
		image.addClickListener(new HeaderClickListener());
		horizontalPanel_4.add(image);
		image.setStyleName("gwt-ImageLink");
		horizontalPanel_4.setCellHorizontalAlignment(image, HasHorizontalAlignment.ALIGN_LEFT);
		image.setSize("282px", "58px");

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setSize("400px", "50px");
		horizontalPanel.setCellHorizontalAlignment(horizontalPanel_1, HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		final HeaderTab headerTab = new HeaderTab();

		final HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		horizontalPanel_1.add(horizontalPanel_3);
		horizontalPanel_3.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_3.setSpacing(4);

		tb.setName("content");
		horizontalPanel_3.add(tb);
		tb.setSize("230px", "25px");
		tb.addKeyboardListener(new KeyboardListenerAdapter() {
		    public void onKeyPress(Widget sender, char keyCode, int modifiers) {
		      if (keyCode == (char) KEY_ENTER){
		    	  doSearch();
		      }
		    }
		  });
		headerTab.setSearchBox(tb);


		final Button searchyAllButton = new Button("searchy-poo-bee", new ClickListener() {
			public void onClick(Widget sender) {
	        	  doSearch();
			}
		});
		horizontalPanel_3.add(searchyAllButton);
		searchyAllButton.setText("search all");
		searchyAllButton.setHeight("25px");
		verticalPanel.add(headerTab);

	}

	private void doSearch(){
		// CAREFUL !!!!!!!!
		// order of search results much match JSONSearchAll in helloworld.py
		// because results are returned in one long json array
		// CAREFUL !!!!!!!!
		SearchResult searchResults[] = new SearchResult[4];
		searchResults[0] = new CodeSearchResult(true);
		searchResults[1] = new WorkshopSearchResult(true);
		searchResults[2] = new ThemeSearchResult(true);
		searchResults[3] = new MaterialSearchResult(true);
		searchResults[0].setStyleName("gwt-SearchResultsPanel");
		searchResults[1].setStyleName("gwt-SearchResultsPanel");
		searchResults[2].setStyleName("gwt-SearchResultsPanel");
		searchResults[3].setStyleName("gwt-SearchResultsPanel");

		String searchText = "/python/searchall?search=" + tb.getText();
		HTTPRequest.asyncGet(searchText, new JSONSearchAllResponseTextHandler(searchResults, tb.getText()));		        	  
	}
	private TextBox tb = new TextBox();
}
